package com.hosp.service;

import java.util.ArrayList;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hosp.bean.Drug;
import com.hosp.bean.Drugpeople;
import com.hosp.bean.DrugpeopleExample;
import com.hosp.mapper.DrugMapper;
import com.hosp.mapper.DrugpeopleMapper;
import com.hosp.vo.BeHospitalVO;
import com.hosp.vo.DrugpeopleVO;
import com.hosp.vo.PageRequestVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * @author ztranscript 2021/10/17 17:12
 **/
@Slf4j
@Service
public class DrugPeopleService {
    @Autowired
    private DrugService drugService;

    @Autowired
    private BeHospitalService beHospitalService;

    @Autowired
    private DrugMapper drugMapper;

    @Autowired
    private DrugpeopleMapper drugpeopleMapper;

    /**
     * 查询列表
     *
     * @param drugpeople  查询条件
     * @param pageRequest 分页条件
     * @return 响应
     */
    public PageInfo<DrugpeopleVO> list(Drugpeople drugpeople, PageRequestVO pageRequest) {
        DrugpeopleExample example = new DrugpeopleExample();
        example.createCriteria().andPeopleidEqualTo(drugpeople.getPeopleid());
        // 分页查询
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        List<Drugpeople> drugpeopleList = drugpeopleMapper.selectByExample(example);
        log.debug("drugpeopleList size = {}.", drugpeopleList.size());
        PageInfo<Drugpeople> drugpeoplePageInfo = new PageInfo<>(drugpeopleList);

        List<DrugpeopleVO> drugpeopleVOList = new ArrayList<>();
        for (Drugpeople dp : drugpeopleList) {
            DrugpeopleVO drugpeopleVO = new DrugpeopleVO();
            BeanUtils.copyProperties(dp, drugpeopleVO);

            // 查询 drug 信息
            Drug drug = drugService.selectById(dp.getDrId());
            drugpeopleVO.setDrug(drug);

            // 查询病人信息
            BeHospitalVO beHospitalVO = beHospitalService.selectById(dp.getPeopleid());
            drugpeopleVO.setPeople(beHospitalVO);

            drugpeopleVOList.add(drugpeopleVO);
        }

        log.debug("drugpeopleVOList size = {}.", drugpeopleVOList.size());
        PageInfo<DrugpeopleVO> pageInfo = new PageInfo<>();
        BeanUtils.copyProperties(drugpeoplePageInfo, pageInfo, "list");
        pageInfo.setList(drugpeopleVOList);

        return pageInfo;
    }

    /**
     * 保存
     *
     * @param drugpeople 保存对象
     * @return 响应
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean save(Drugpeople drugpeople) {
        DrugpeopleExample example = new DrugpeopleExample();
        DrugpeopleExample.Criteria criteria = example.createCriteria();
        // 根据<病人ID,药品ID>维度查询
        criteria.andPeopleidEqualTo(drugpeople.getPeopleid());
        criteria.andDrIdEqualTo(drugpeople.getDrId());
        List<Drugpeople> drugpeopleList = drugpeopleMapper.selectByExample(example);

        if (CollectionUtils.isEmpty(drugpeopleList)) {
            // 新增
            drugpeople.setDrSendNumber(0);
            drugpeopleMapper.insertSelective(drugpeople);
        } else {
            Integer drNumberDb = drugpeopleList.get(0).getDrNumber();
            drugpeople.setDrNumber(drugpeople.getDrNumber() + drNumberDb);
            // 修改
            drugpeopleMapper.updateByExampleSelective(drugpeople, example);
        }

        return true;
    }

    /**
     * 主键查询
     *
     * @param drId 查询条件
     * @return 响应
     */
    public Drug selectById(Integer drId) {
        return drugMapper.selectByPrimaryKey(drId);
    }

    /**
     * 删除
     *
     * @param uIds 待删除 IDs
     * @return 响应
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(List<Integer> uIds) {
        return true;
    }

}
